import { describe, expect, test } from 'vitest';
import { urlToObj } from '../../src/hammer/url.mts';

describe('URL工具函数', () => {
  describe('parseUrlParams', () => {
    test('基本URL参数解析', () => {
      const url = 'https://example.com?name=john&age=25';
      const result = urlToObj(url);
      expect(result).toEqual({ name: 'john', age: '25' });
    });

    test('包含多个参数的URL解析', () => {
      const url =
        'https://example.com?id=123&user=admin&role=manager&active=true';
      const result = urlToObj(url);
      expect(result).toEqual({
        id: '123',
        user: 'admin',
        role: 'manager',
        active: 'true',
      });
    });

    test('包含特殊字符的URL参数解析', () => {
      const url =
        'https://example.com?search=hello%20world&category=books&tag=sci-fi';
      const result = urlToObj(url);
      expect(result).toEqual({
        search: 'hello%20world',
        category: 'books',
        tag: 'sci-fi',
      });
    });

    test('空参数值的处理', () => {
      const url = 'https://example.com?name=&token=&visible=false';
      const result = urlToObj(url);
      expect(result).toEqual({
        name: '',
        token: '',
        visible: 'false',
      });
    });

    test('无参数的URL处理', () => {
      expect(() => {
        urlToObj('https://example.com');
      }).toThrow();
    });

    test('重复参数名的处理', () => {
      const url = 'https://example.com?id=1&id=2&name=test';
      const result = urlToObj(url);
      // 当有重复参数时，后面的值会覆盖前面的值
      expect(result).toEqual({
        id: '2',
        name: 'test',
      });
    });
  });
});
